package cn.iocoder.yudao.module.famous.doctor.controller.app.inquiry;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.archives.vo.ArchivesRespVO;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.inquiry.vo.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroupuser.vo.ResearchgroupUserByClinic;
import cn.iocoder.yudao.module.famous.doctor.convert.archives.ArchivesConvert;
import cn.iocoder.yudao.module.famous.doctor.convert.inquiry.InquiryConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.archives.ArchivesDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.casehistory.CaseHistoryDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.inquiry.InquiryDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.prescription.PrescriptionDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.researchgroupuser.ResearchgroupUserDO;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.treatmentuser.TreatmentUserDO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.archives.ArchivesMapper;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.casehistory.CaseHistoryMapper;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.researchgroupuser.ResearchgroupUserMapper;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.treatmentuser.TreatmentUserMapper;
import cn.iocoder.yudao.module.famous.doctor.service.inquiry.InquiryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 我的问诊")
@RestController
@RequestMapping("/famous.doctor/inquiry/app")
@Validated
public class AppInquiryController {

    @Resource
    private InquiryService inquiryService;
    @Resource
    private ResearchgroupUserMapper researchgroupUserMapper;
    @Resource
    private TreatmentUserMapper treatmentUserMapper;

    @Resource
    private ArchivesMapper archivesMapper;


    @PostMapping("/create")
    @Operation(summary = "创建我的问诊")
    public CommonResult<Long> createInquiry(@Valid @RequestBody InquiryCreateReqVO createReqVO) {
        return success(inquiryService.createInquiry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的问诊")
    public CommonResult<Boolean> updateInquiry(@Valid @RequestBody InquiryUpdateReqVO updateReqVO) {
        inquiryService.updateInquiry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的问诊")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteInquiry(@RequestParam("id") Long id) {
        inquiryService.deleteInquiry(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的问诊")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<InquiryRespVO> getInquiry(@RequestParam("id") Long id) {
        InquiryDO inquiry = inquiryService.getInquiry(id);
        return success(InquiryConvert.INSTANCE.convert(inquiry));
    }

    @PostMapping("/getDiagnoseLabel")
    @Operation(summary = "复诊获取初诊信息")
    public CommonResult<InquiryRespVO> getDiagnoseLabel(@Valid @RequestBody InquiryReqLabelVO exportReqVO) {
        InquiryDO inquiry = inquiryService.getDiagnoseLabel(exportReqVO);
        if (ObjectUtil.isEmpty(inquiry)){
            return error(999999,"该诊断人没有初诊记录");
        }
        return success(InquiryConvert.INSTANCE.convert(inquiry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得我的问诊列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<InquiryRespVO>> getInquiryList(@RequestParam("ids") Collection<Long> ids) {
        List<InquiryDO> list = inquiryService.getInquiryList(ids);
        return success(InquiryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的问诊分页")
    public CommonResult<PageResult<InquiryRespVO>> getInquiryPage(@Valid InquiryPageReqVO pageVO) {
        PageResult<InquiryDO> pageResult = inquiryService.getInquiryPage(pageVO);
        return success(InquiryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getMyPatient")
    @Operation(summary = "医生-我的患者")
    public CommonResult<IPage<InquiryMyPatientRespVO>> getMyPatient(@Valid InquiryMyPatientPageReqVO pageVO) {
        IPage<InquiryMyPatientRespVO> pageResult = inquiryService.getMyPatient(pageVO);
        pageResult.getRecords().forEach(inquiryMyPatientRespVO -> {
            List<ResearchgroupUserByClinic> researchgroupUserByClinics = researchgroupUserMapper.selectClinicList(4L, inquiryMyPatientRespVO.getIdentityCard());
            if (researchgroupUserByClinics.size()>0) {
                inquiryMyPatientRespVO.setIsClinic(true);
            }else {
                inquiryMyPatientRespVO.setIsClinic(false);
            }
        });
        return success(pageResult);
    }

    @GetMapping("/getMyPatientByid")
    @Operation(summary = "医生-患者档案")
    public CommonResult<InquiryMyPatientByidRespVO> getMyPatientByid(@RequestParam("id") Long id) {
        //返回对象
        InquiryMyPatientByidRespVO inquiryMyPatientByidRespVO = new InquiryMyPatientByidRespVO();
        //查询问诊信息
        InquiryDO inquiry = inquiryService.getInquiry(id);
        String creator = inquiry.getCreator();
        List<InquiryMyPatientRespVO> inquiryRespVOS=inquiryService.selectAllFormByUserids(creator,inquiry.getIdentityCard()); //患者病历
        //患者病历 （详情 + 咨询次数 ）
        if (inquiryRespVOS.size()>0){
            inquiryMyPatientByidRespVO.setCaseHistorys(inquiryRespVOS);
            inquiryMyPatientByidRespVO.setCaseHistorynum((long) inquiryRespVOS.size());
        }
        List<ResearchgroupUserByClinic> researchgroupUserByClinics = researchgroupUserMapper.selectClinicList(4L, inquiry.getIdentityCard());

        if (researchgroupUserByClinics.size()>0) {
            inquiryMyPatientByidRespVO.setIsClinic(true);
        }else {
            inquiryMyPatientByidRespVO.setIsClinic(false);
        }
        //就诊人信息
        TreatmentUserDO treatmentUserDO=treatmentUserMapper.selectUserBycard(creator,inquiry.getIdentityCard());
        inquiryMyPatientByidRespVO.setAge(Long.valueOf(treatmentUserDO.getAge())).setHeight(treatmentUserDO.getHeight()).setSex(Long.valueOf(treatmentUserDO.getSex()))
                .setJzName(treatmentUserDO.getName()).setWeight(treatmentUserDO.getWeight()).setOccupation(treatmentUserDO.getOccupation())
                .setIdentityCard(treatmentUserDO.getIdentityCard()).setJzCreator(creator);

        ArchivesDO archivesDO = archivesMapper.selectOneBycard(creator, inquiry.getIdentityCard(),getLoginUserId());
        ArchivesRespVO convert = ArchivesConvert.INSTANCE.convert(archivesDO);
        if (archivesDO!=null) {
            inquiryMyPatientByidRespVO.setArchivesDO(convert);
        }
        return success(inquiryMyPatientByidRespVO);
    }

    @GetMapping("/getIoutpatientList")
    @Operation(summary = "获得就诊列表分页")
    public CommonResult<PageResult<InquiryRespVO>> getIoutpatientList(@Valid InquiryPageReqVO pageVO) {
        PageResult<InquiryDO> pageResult = inquiryService.getIoutpatientList(pageVO);
        return success(InquiryConvert.INSTANCE.convertPage(pageResult));
    }
}
